package top.suven.base.http.admin.tools.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.suven.base.http.admin.tools.constant.*;
import top.suven.base.http.admin.tools.dao.BuildCodeDao;
import top.suven.base.http.admin.tools.model.BuildCodeColumn;
import top.suven.base.http.admin.tools.service.BuildCodeService;
import top.suven.base.http.admin.tools.vo.request.BuildCodeRequestVo;
import top.suven.base.http.admin.tools.vo.response.BuildCodeResponseVo;
import top.suven.base.util.utilize.BeanUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName:
 * @Description:
 * @Author lixiangling
 * @Date 2018/7/10 15:45
 * @Copyright: (c) 2018 gc by https://www.gc.com.cn/
 * @Version : 1.0.0
 * --------------------------------------------------------
 * modifyer    modifyTime                 comment
 * <p>
 * --------------------------------------------------------
 */
@Component("buildCodeService")
public class BuildCodeServiceImpl implements BuildCodeService {
    @Autowired
    private BuildCodeDao buildCodeDao;
    @Override
    public List<BuildCodeResponseVo> getBuildCodeByTableName(String tableName) {
        List<BuildCodeColumn> buildCodeColumns = buildCodeDao.getBuildCodeByTableName(tableName);
        if(buildCodeColumns == null || buildCodeColumns.isEmpty()){
            return Collections.EMPTY_LIST;
        }
        List<BuildCodeResponseVo> buildCodeResponseVos = new ArrayList<>();
        List<String> pkNameList = buildCodeDao.getPKName(tableName);
        for (BuildCodeColumn buildCodeColumn:buildCodeColumns){
            BuildCodeResponseVo buildCodeResponseVo = new BuildCodeResponseVo();
            BeanUtil.copyProperties(buildCodeColumn, buildCodeResponseVo);
            buildCodeResponseVo.setEditFlagValue("");
            buildCodeResponseVo.setQueryFlagValue("");
            buildCodeResponseVo.setShowFlagValue("");
            String columnComment = buildCodeResponseVo.getColumnComment();
            buildCodeResponseVo.setColumnComment(StringUtils.isBlank(columnComment)?"":columnComment);
            buildCodeResponseVo.setPropertyLength(buildCodeColumn.getLength()==-1?"":String.valueOf(buildCodeColumn.getLength()-4));
            buildCodeResponseVo.setPropertyName(this.relaceColumnToPropertyName(buildCodeResponseVo.getColumnName()));
            buildCodeResponseVo.setJavaType(getJavaType(buildCodeResponseVo.getDbType(),buildCodeResponseVo.getColumnName()));
            buildCodeResponseVo.setShowList(true);
            buildCodeResponseVo.setShowFlag(TableShowFlag.VALUE.name());
            if(buildCodeResponseVo.getPropertyName().equals("status")){
                buildCodeResponseVo.setShowList(false);
            }else if(buildCodeResponseVo.getPropertyName().equals("enableStatus")){
                buildCodeResponseVo.setShowFlag(TableShowFlag.TRANS.name());
                buildCodeResponseVo.setShowFlagTransType(TransType.JS.name());
                buildCodeResponseVo.setShowFlagValue("enableStatusTpl");
            }
            buildCodeResponseVo.setShowQuery(false);
            buildCodeResponseVo.setQueryFlag(FormShowFlag.TEXT.name());
            buildCodeResponseVo.setQueryType(QueryType.EQUAL.name());
            buildCodeResponseVo.setShowEdit(true);
            buildCodeResponseVo.setEditFlag(FormShowFlag.TEXT.name());
            if(pkNameList.contains(buildCodeResponseVo.getColumnName())){
                buildCodeResponseVo.setPK(true);
                buildCodeResponseVo.setShowEdit(false);
                buildCodeResponseVo.setShowQuery(false);
            }
            this.setRegular(buildCodeResponseVo);
            buildCodeResponseVos.add(buildCodeResponseVo);
        }
        return buildCodeResponseVos;
    }

    @Override
    public void createCode(BuildCodeRequestVo buildCodeRequestVo) {

    }

    /**
     * @Title: 数据库字段转java属性
     * @Description: 
     * @param  columnName {@Link String} 字段名
     * @return  
     * @throw 
     * @author lixiangling
     * @date 2018/7/12 10:09
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *  
     *  --------------------------------------------------------
     */
    private String relaceColumnToPropertyName(String columnName){
        StringBuffer sb = new StringBuffer();
        Pattern p = Pattern.compile("_[a-z|A-Z]");
        Matcher m = p.matcher(columnName);
        while (m.find())
        {
            m.appendReplacement(sb,  m.group().toUpperCase());
        }
        m.appendTail(sb);
        return sb.toString().replaceAll("_", "");
    }
    /**
     * @Title: 获取JAVA类型
     * @Description:
     * @param  columnType {@Link String} 数据库类型
     * @param  columnName {@Link String} 数据库字段名
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/7/12 10:27
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    private String getJavaType(String columnType,String columnName){
        String returnType = "String";
        if(columnType.startsWith("int4") ){
            returnType = "long";
        }else if (columnType.endsWith("int2")) {
            returnType = "int";
        }else if (columnName.endsWith("status")) {
            returnType = "int";
        }else if (columnType.startsWith("timestamp")) {
            returnType = "Date";
        }
        return returnType;
    }

    private void setRegular(BuildCodeResponseVo buildCodeResponseVo){
        if("String".equals(buildCodeResponseVo.getJavaType())){
            buildCodeResponseVo.setVerifyType(VerifyType.REGULAR.name());
            if(buildCodeResponseVo.isNotNull()){
                buildCodeResponseVo.setJavaFormRegularMessage(new StringBuffer().append(buildCodeResponseVo.getColumnComment()).
                        append("不能为空且长度不能超过").append(buildCodeResponseVo.getPropertyLength()).append("位").toString());
                buildCodeResponseVo.setJsFormRegularMessage(new StringBuffer().append(buildCodeResponseVo.getColumnComment()).
                        append("不能为空且长度不能超过").append(buildCodeResponseVo.getPropertyLength()).append("位").toString());
                buildCodeResponseVo.setJavaFormRegular(new StringBuffer("^.{1,").append(buildCodeResponseVo.getPropertyLength()).append("}").toString());
                buildCodeResponseVo.setJsFormRegular(new StringBuffer("!/^.{1,").append(buildCodeResponseVo.getPropertyLength()).append("}$/").toString());
            }else{
                buildCodeResponseVo.setVerifyType(VerifyType.REGULAR.name());
                buildCodeResponseVo.setJavaFormRegular(new StringBuffer("^.{0,").append(buildCodeResponseVo.getPropertyLength()).append("}").toString());
                buildCodeResponseVo.setJavaFormRegularMessage(new StringBuffer().append(buildCodeResponseVo.getColumnComment()).
                        append("不能超过").append(buildCodeResponseVo.getPropertyLength()).append("位").toString());
                buildCodeResponseVo.setJsFormRegular(new StringBuffer("!/^.{0,").append(buildCodeResponseVo.getPropertyLength()).append("}$/").toString());
                buildCodeResponseVo.setJsFormRegularMessage(new StringBuffer().append(buildCodeResponseVo.getColumnComment()).
                        append("不能超过").append(buildCodeResponseVo.getPropertyLength()).append("位").toString());
            }
        }else if("int".equals(buildCodeResponseVo.getJavaType())){
            buildCodeResponseVo.setVerifyType(VerifyType.MAX.name());
            buildCodeResponseVo.setJavaFormRegular(new StringBuffer().append(Integer.MAX_VALUE).toString());
            buildCodeResponseVo.setJavaFormRegularMessage(new StringBuffer().append(buildCodeResponseVo.getColumnComment()).
                    append("不能超过").append(Integer.MAX_VALUE).toString());
            buildCodeResponseVo.setJsFormRegular(new StringBuffer().append(Integer.MAX_VALUE).toString());
            buildCodeResponseVo.setJsFormRegularMessage(new StringBuffer().append(buildCodeResponseVo.getColumnComment()).
                    append("必须是").append(Integer.MAX_VALUE).append("以内的正整数").toString());
        }else if("long".equals(buildCodeResponseVo.getJavaType())){
            buildCodeResponseVo.setVerifyType(VerifyType.MAX.name());
            buildCodeResponseVo.setJavaFormRegular(new StringBuffer().append(Integer.MAX_VALUE).toString());
            buildCodeResponseVo.setJavaFormRegularMessage(new StringBuffer().append(buildCodeResponseVo.getColumnComment()).
                    append("不能超过").append(Integer.MAX_VALUE).toString());
            buildCodeResponseVo.setJsFormRegular(new StringBuffer().append(Integer.MAX_VALUE).toString());
            buildCodeResponseVo.setJsFormRegularMessage(new StringBuffer().append(buildCodeResponseVo.getColumnComment()).
                    append("必须是").append(Integer.MAX_VALUE).append("以内的正整数").toString());
        }else if("Date".equals(buildCodeResponseVo.getJavaType())){
            buildCodeResponseVo.setVerifyType(VerifyType.DATE.name());
            buildCodeResponseVo.setJavaFormRegular("yyyy-MM-dd");
            buildCodeResponseVo.setJavaFormRegularMessage(new StringBuffer().append(buildCodeResponseVo.getColumnComment()).append("时间格式不正确").toString());
            buildCodeResponseVo.setJsFormRegular(new StringBuffer("DateTime.TryParse").toString());
            buildCodeResponseVo.setJsFormRegularMessage(new StringBuffer().append(buildCodeResponseVo.getColumnComment()).
                    append("时间格式不正确").toString());
        }
    }
}
